<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <el-table :data="rolesList" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row v-for="LevelOne,index in scope.row.children" :key="LevelOne.id" :class="['bdbottom',index==0?'bdtop':'','vcenter']">
              <!-- 一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="removeRightById(scope.row,LevelOne.id)">{{LevelOne.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级权限 -->
              <el-col :span="19">
                <el-row :class="[index==0?'':'bdtop','vcenter']" v-for="levelTwo,index in LevelOne.children" :key="levelTwo.id">
                  <el-col :span="6">
                    <el-tag type="success" closable @close="removeRightById(scope.row,levelTwo.id)">{{levelTwo.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <!-- 三级权限 -->
                    <el-tag type="warning" v-for="levelThree in levelTwo.children" :key="levelThree.id" closable @close="removeRightById(scope.row,levelThree.id)">
                      {{levelThree.authName}}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting" @click="showSetRightDialog(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限弹出框 -->
    <el-dialog title="分配权限" :visible.sync="setRightDialogVisible" width="50%" @close="setRightDialogClose">
      <el-tree ref="treeRef" :data="rightsList" :props="treeProps" show-checkbox node-key="id" default-expand-all :default-checked-keys="defKyes"></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allotRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      treeProps: {
        label: "authName",
        children: "children",
      },
      defKyes: [],
      rolesList: [],
      setRightDialogVisible: false,
      rightsList: [],
      roleId: "",
    };
  },
  created() {
    this.getRolesList();
  },
  methods: {
    async getRolesList() {
      const { data } = await this.$http.get("roles");
      if (data.meta.status == 200) {
        this.rolesList = data.data;
      } else {
        return this.$message.error("获取角色列表失败");
      }
    },
    removeRightById(role, rightId) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data } = await this.$http.delete(
            `roles/${role.id}/rights/${rightId}`
          );

          if (data.meta.status == 200) {
            this.$message({
              type: "success",
              message: "成功删除权限!",
            });
            role.children = data.data;
          } else {
            this.$message.error("权限删除失败");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async showSetRightDialog(role) {
      this.roleId = role.id;
      //获取权限数据
      const { data } = await this.$http.get("rights/tree");
      if (data.meta.status == 200) {
        this.rightsList = data.data;
        this.getleafKeys(role, this.defKyes);
        this.setRightDialogVisible = true;
      } else {
        this.$message.error("获取数据失败");
      }
    },
    getleafKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((item) => {
        this.getleafKeys(item, arr);
      });
    },
    setRightDialogClose() {
      this.defKyes = [];
    },
    async allotRights() {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys(),
      ];
      const idStr = keys.join(",");
      const { data } = await this.$http.post(`roles/${this.roleId}/rights`, {
        rids: idStr,
      });
      if (data.meta.status != 200) return this.$message.error("修改权限失败");
      this.$message.success("权限修改成功");
      this.getRolesList();
      this.setRightDialogVisible = false;
    },
  },
};
</script>

<style lang="less">
.vcenter {
  display: flex;
  align-items: center;
}
</style>